<template>
  <div>
    <div>文章类型</div>
    <div class="essayG">文章管理/文章类型</div>
    <div class="essayBox">
      <span class="headWord">时间：</span>
      <el-date-picker 
      v-model="chooseList.time" 
      type="date" 
      placeholder="选择日期">
      </el-date-picker>
      <span class="headWord">标题：</span>
      <el-input
        v-model="chooseList.title"
        placeholder="请输入内容"
        class="headTitle"
      ></el-input>
      <span class="headWord">类型：</span>
      <el-select v-model="chooseList.value" placeholder="请选择">
        <el-option
          v-for="item in types"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <span class="headWord">状态：</span>
      <el-select v-model="chooseList.status" placeholder="请选择">
        <el-option
          v-for="item in statuses"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <button class="choose" @click="choose">筛选</button>
      <button class="add" @click="add">添加</button>
    </div>
    <template>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="essayId" label="文章ID" width="180">
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column label="头条" width="180">
          <el-switch
            v-model="headT"
            active-color="#13ce66"
            inactive-color="#666666"
          >
          </el-switch>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="180"></el-table-column>
        <el-table-column
          prop="time"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column prop="orders" label="排序"> </el-table-column>
        <el-table-column prop="status" label="状态"> </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-drawer title="编辑" :visible.sync="drawer" :with-header="false">
      <div class="addBox">
        <div class="addHead">{{drawerAdd?"编辑":"添加"}}</div>
        <p>标题:</p>
        <el-input v-model="addList.title" placeholder="请输入内容"></el-input>
        <p class="headWord">类型：</p>
        <el-select v-model="addList.type" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <p>封面：</p>
        <el-upload
          class="avatar-uploader"
          action="http://192.168.31.39:9101/ad/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <p class="tips">
          支持格式：JPG/PNG ，单个文件不能超过10MB，建议尺寸：300x400
        </p>
        <p>来源:</p>
        <el-input v-model="addList.source" placeholder="请输入来源"></el-input>
        <p>内容:</p>
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="addList.content"
        >
        </el-input>
        <p>排序:</p>
        <el-input v-model="addList.orders" placeholder="请输入内容"></el-input>
        <p>状态:</p>
        <el-radio v-model="addList.status" label="启用">启用</el-radio>
        <el-radio v-model="addList.status" label="禁用">禁用</el-radio>
        <div class="btn">
          <el-button type="primary" @click="putTrue">确认</el-button>
          <el-button type="info" @click="FalseT">取消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      currentPage: 1,
      pagesize: 2,
      list: [],
      total: 0,
      drawer: false,
      drawerAdd: true,
      
      addList: {},
      options: [
        {
          value: "游戏类",
          label: "游戏类",
        },
        {
          value: "生活类",
          label: "生活类",
        },
        {
          value: "健康类",
          label: "健康类",
        },
      ],
      types: [
        {
          value: "游戏类",
          label: "游戏类",
        },
        {
          value: "生活类",
          label: "生活类",
        },
        {
          value: "健康类",
          label: "健康类",
        },
      ],
      statuses: [
        {
          value: "启用",
          label: "启用",
        },
        {
          value: "禁用",
          label: "禁用",
        },
      ],
      value: "",
      imageUrl: "",
      type: "",
      status: "",
      name: "",
      headT: true,
      textarea:"",
      chooseList:{}
    };
  },
  methods: {
    //编辑
    handleEdit(index, row) {
      this.drawer = true;
      this.addList={...row}
      console.log(index, row);
    },
    // 提示弹框
    open2(msg) {
      this.$message({
        message: `${msg}成功`,
        type: "success",
      });
    },
    //删除按钮
    handleDelete(index, row) {
      console.log(index, row.adId);
      // axios.get(`http://192.168.31.39:9101/ad/del/${row.adId}`).then((res) => {
      //   if (res.data.code == 200) {
      //     this.open2("删除");
      //     this.getList(this.currentPage, this.pagesize);
      //   }
      // });
    },
    //获取数据
    getList(currentPage, pagesize) {
      axios
        .get(
          `http://192.168.31.39:9101/essay/findAll/${currentPage}/${pagesize}`
        )
        .then((res) => {
          // console.log(res);
          this.total = res.data.data.total;
          this.list = res.data.data.data;
          this.list.map((item) => {
            if (item.status == 1) {
              item.status = "启用";
            } else {
              item.status = "禁用";
            }
          });
          // console.log(this.list);
        });
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.getList(this.currentPage, this.pagesize);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList(this.currentPage, this.pagesize);
    },
    //筛选
    choose() {
      console.log(this.chooseList);
      axios({
        url:"",
        method:"post",
        data:chooseList,
      }).then(res=>{
        console.log(res);
      })
      this.chooseList={}
    },
    add() {
      this.drawer=true
      this.drawerAdd=false
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log("this.imageUrl", this.imageUrl);
    },
    beforeAvatarUpload(file) {
      console.log("this.imageUrlthis.imageUrlthis.imageUrl");
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      console.log(isJPG, isLt2M, file);
      return isJPG && isLt2M && file;
    },
    //模态框确定按钮
    putTrue() {
      // console.log(this.addList);
      // this.drawer = false;
      // this.drawerAdd = true;
      this.addList["type"] = this.value;
      let list = { ...this.addList };
      if (this.list.status == "启用") {
        list.status = 1;
      } else {
        list.status = 0;
      }
      console.log("addList", list);
      if (this.drawerAdd) {
        this.editList(list);
      } else {
        this.addT(list);
      }
      this.drawer = false;
      this.addList={}
    },
    //取消按钮
    FalseT() {
      this.drawer = false;
      this.drawerAdd = true;
      this.addList={}
    },
    //编辑修改请求
    editList(list){
      axios({
        url:"",
        method:"post",
        data:list,
      }).then(res=>{
        console.log(res);
      })
    },
    //修改请求
    addT(list){
      axios({
        url:"",
        method:"post",
        data:list,
      }).then(res=>{
        console.log(res);
      })
    }
  },
  created() {
    this.getList(this.currentPage, this.pagesize);
  },
};
</script>
<style scoped>
.choose {
  background-color: rgb(48, 208, 198);
  color: white;
  border: none;
  height: 40px;
  width: 50px;
  border-radius: 3px;
  margin-left: 7px;
}
.add {
  background-color: rgb(48, 208, 198);
  color: white;
  border: none;
  height: 40px;
  width: 60px;
  position: absolute;
  right: 0px;
  border-radius: 3px;
}
.essayG {
  margin: 10px 0;
}
.essayBox {
  margin-bottom: 10px;
  position: relative;
}
.headTitle {
  width: 200px;
}
.addHead {
  text-align: center;
}
.addBox {
  padding: 20px 10px;
}
.btn {
  margin-top: 20px;
  text-align: center;
}
.tips {
  font-size: 12px;
  color: dimgrey;
}
</style>